<!--
 * @author wangjinqiu
 -->
<template>
  <div class="app-container hundredLearningEdit">
    <div class="main-title">新建/编辑</div>
    <el-form
      ref="baseform"
      :model="baseform"
      :rules="baserules"
      label-width="160px"
      label-position="left"
      class="dataForm"
    >
      <el-form-item label="课程名称" prop="name">
        <el-input v-model="baseform.name" />
      </el-form-item>
      <el-form-item label="简介" required>
        <tinymce v-model="baseform.remark" :height="300" />
      </el-form-item>
      <el-form-item label="课程封面" required>
        <div class="custom-uploader" @click="imagecropperShow=true">
          <img v-if="baseform.imageId" :src="view + baseform.imageId">
          <template v-else>
            <div class="icon-wrap">
              <i class="el-icon-plus avatar-uploader-icon" />
            </div>
          </template>
          <p class="size-tip">建议上传尺寸为：800px*520px</p>
        </div>
        <image-cropper
          v-show="imagecropperShow"
          :key="imagecropperKey"
          :width="800"
          :height="520"
          :url="uploadaction"
          :headers="uploadHeaders"
          :no-circle="true"
          lang-type="zh"
          @close="close"
          @crop-upload-success="cropSuccessImg"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button style="padding: 8px 30px;" @click="cancel">取消</el-button>
      <el-button style="padding: 8px 30px;" type="primary" @click="insertOrUpdate">确定
      </el-button>
    </div>
  </div>
</template>
<script>
import { specialText, number } from '@/utils/validate'
import ImageCropper from '@/components/ImageCropper'
// import { courseGetInfo,courseInsert,courseUpdate } from '@/api/hundredLearning/index'
import { getToken } from '@/utils/auth'
import Tinymce from '@/components/Tinymce'
export default {
  name: 'PunchClockCourseEdit',
  components: { ImageCropper, Tinymce },
  data() {
    return {
      id: '', // 活动ID
      uploadaction: '/API-RESOURCE/upload/uploadFile',
      uploadHeaders: { 'ttsxAuthToken': getToken() },
      view: '/API-RESOURCE/webView?fid=',
      imagecropperKey: 0,
      imagecropperShow: false,
      baseform: {
        name: '',
        imageId: '',
        remark: ''
      },
      state: '',
      baserules: {
        name: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
          { validator: specialText, trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        seasonInfo: [
          { required: true, message: '请填写课程简介', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.id = this.$route.query.id || ''
    this.state = this.$route.query.type || ''
    // this.id && this.getBaseData()
  },
  methods: {
    close() {
      this.imagecropperShow = false
    },
    cropSuccessImg(resData) {
      console.log(resData)
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.$set(this.baseform, 'imageId', resData[0].id)
    },
    cancel() {
      this.$router.push({ path: this.$filterPath('course') })
    },
    insertOrUpdate() {
      this.$refs['baseform'].validate((valid) => {
        if (valid) {
          const postData = Object.assign({}, this.baseform)
          if (!this.baseform.imageId) {
            this.$message.error('请上传课程封面')
            return
          }
          // postData.imageId = this.image && JSON.stringify(this.image)
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.1)'
          })
          if (this.state == 'c') {
            return new Promise((resolve, reject) => {
              courseInsert(postData).then(() => {
                loading.close()
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 1500
                })
                this.$router.push({ path: this.$filterPath('course') })
              }).catch(error => {
                reject(error)
                loading.close()
              })
            })
          } else {
            return new Promise((resolve, reject) => {
              courseUpdate(postData).then(() => {
                loading.close()
                this.$notify({
                  title: '成功',
                  message: '更新成功',
                  type: 'success',
                  duration: 1500
                })
                this.$router.push({ path: this.$filterPath('course') })
              }).catch(error => {
                reject(error)
                loading.close()
              })
            })
          }
        }
      })
    },
    // 获取详情
    getBaseData() {
      var id = this.id
      return new Promise((resolve, reject) => {
        courseGetInfo(id).then(res => {
          this.baseform = Object.assign(this.baseform, res.data.data)
          // this.image = res.data.data.imageId && JSON.parse(res.data.data.imageId)
          // console.log(this.image)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
$mainColor: #18a5fd;
.el-tree {
  overflow-y: auto;
  height: 300px;
}
.hundredLearningEdit {
      background-color: #fff;
  .main-title{
    position: relative;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    background-color: #ffffff;
    border-bottom: 1px solid #E5E5E5;
    font-family:PingFangSC-Medium;
    font-weight: bold;
    color:rgba(51,51,51,1);
    font-size: 14px;
    &:before{
      content: "";
      position: absolute;
      width: 2px;
      background-color: $mainColor;
      top: 13px;
      bottom: 13px;
      left: 15px;
    }
  }
  .select-data {
    display: inline-block;
    background: rgba(246, 246, 246, 1);
    border-radius: 4px;
    font-size: 14px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(51, 51, 51, 1);
    padding: 0 10px;
    margin-right: 5px;
    margin-bottom: 8px;
    i {
      cursor: pointer;
      padding-left: 5px;
    }
  }
  .selectbtn {
    display: inline-block;
    width: 120px;
    background: rgba(255, 255, 255, 1);
    border: 1px dotted $mainColor;
    border-radius: 4px;
    font-size: 14px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: $mainColor;
    text-align: center;
    cursor: pointer;
  }
  .cerate-step {
    background-color: #ffffff;
    .step-item {
      text-align: center;
      height: 60px;
      line-height: 60px;
      .step-item-num {
        display: inline-block;
        width: 35px;
        height: 35px;
        line-height: 35px;
        border: 1px solid rgba(240, 240, 240, 1);
        border-radius: 50%;
        font-size: 12px;
        font-family: PingFangSC-Medium;
        font-weight: 500;
        color: rgba(198, 198, 198, 1);
        margin-right: 5px;
      }
      .step-item-txt {
        font-size: 14px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(153, 153, 153, 1);
      }
      i {
        font-size: 40px;
        color: $mainColor;
        vertical-align: middle;
      }
      &.active {
        .step-item-num {
          background: $mainColor;
          border: 1px solid $mainColor;
          color: #ffffff;
        }
        .step-item-txt {
          color: $mainColor;
        }
      }
    }
    .first {
      position: relative;
      &:after {
        content: "";
        height: 1px;
        background-color: #f0f0f0;
        position: absolute;
        left: 80%;
        right: -20%;
        top: 50%;
      }
    }
  }
  .create-step-con {
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px 0;
    min-height: 500px;
    .baseform {
      width: 1000px !important;
      margin: 0 auto;
      .el-select {
        width: 100%;
      }
    }

    &.step-two {
      position: relative;
      .down-btn {
        margin-top: 30px;
        text-align: center;
      }
    }
    &.step-three {
      .del-col {
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .down-btn {
        margin-top: 30px;
        text-align: center;
      }
    }
  }
  .dataForm{
    width: 800px;
    margin: 20px auto 0;
  }
  .dialog-footer {
    text-align: center;
  }
}
</style>
